package com.ecoolex.ying.space.core.manager;

import com.ecoolex.framework.common.enums.ResultCode;
import com.ecoolex.framework.common.util.BizAssert;
import com.ecoolex.ying.space.core.dao.DesignerProjectAttributeDAO;
import com.ecoolex.ying.space.entity.DesignerProject;
import com.ecoolex.ying.space.entity.DesignerProjectAttribute;
import com.ecoolex.ying.space.entity.DesignerProjectAttribute.Column;
import com.ecoolex.ying.space.entity.DesignerProjectAttributeExample;
import com.ecoolex.ying.space.pojo.EditDesignerProjectAttributeDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.util.Date;

@Service
public class DesignerProjectAttributeManager {
    @Autowired
    private DesignerProjectAttributeDAO dao;

    void copy(Integer sourceProjectId, DesignerProject project, String createUser) {
        DesignerProjectAttribute record = getByProjectId(sourceProjectId);
        record.setProjectId(project.getProjectId());
        record.setCreateUser(createUser);
        record.setCreateTime(Date.from(Instant.now()));
        record.setUpdateUser(createUser);
        record.setUpdateTime(Date.from(Instant.now()));
        dao.insert(record);
    }

    void create(DesignerProject project, String createUser) {
        DesignerProjectAttribute record = new DesignerProjectAttribute();
        record.setProjectId(project.getProjectId());
        record.setUserId(project.getUserId());
        record.setDeleted(DesignerProjectAttribute.NOT_DELETED);
        record.setCreateUser(createUser);
        record.setCreateTime(Date.from(Instant.now()));
        record.setUpdateUser(createUser);
        record.setUpdateTime(Date.from(Instant.now()));
        dao.insert(record);
    }

    void delete(Integer userId, Integer projectId, String updateUser) {
        DesignerProjectAttribute record = new DesignerProjectAttribute();
        record.setDeleted(DesignerProjectAttribute.IS_DELETED);
        record.setUpdateUser(updateUser);
        record.setUpdateTime(Date.from(Instant.now()));

        DesignerProjectAttributeExample example = new DesignerProjectAttributeExample();
        example.createCriteria()
                .andProjectIdEqualTo(projectId)
                .andUserIdEqualTo(userId)
                .andDeletedEqualTo(DesignerProjectAttribute.NOT_DELETED);

        int row = dao.updateByExampleSelective(record,
                example,
                Column.deleted,
                Column.updateUser,
                Column.updateTime);
        BizAssert.isTrue(row == 1, ResultCode.BIZ.build(20), "删除设计师项目属性失败");
    }

    public void update(Integer userId, Integer projectId, EditDesignerProjectAttributeDTO edit, String updateUser) {
        DesignerProjectAttribute record = new DesignerProjectAttribute();
        record.setPageHeader(edit.getPageHeader());
        record.setUpdateUser(updateUser);
        record.setUpdateTime(Date.from(Instant.now()));

        DesignerProjectAttributeExample example = new DesignerProjectAttributeExample();
        example.createCriteria()
                .andProjectIdEqualTo(projectId)
                .andUserIdEqualTo(userId)
                .andDeletedEqualTo(DesignerProjectAttribute.NOT_DELETED);

        int row = dao.updateByExampleSelective(record,
                example,
                Column.pageHeader,
                Column.updateUser,
                Column.updateTime);
        BizAssert.isTrue(row == 1, ResultCode.BIZ.build(20), "修改设计师项目属性失败");
    }

    public DesignerProjectAttribute getByProjectId(Integer projectId) {
        return dao.selectByPrimaryKeyWithLogicalDelete(projectId, DesignerProjectAttribute.NOT_DELETED);
    }
}